import { executeSql } from '@/uni_modules/stars-UniTS-SQLite-ORM/components/stars-UniTS-SQLite-ORM/dbOrm/db/SqliteHelper';
import { DB_NAME } from '@/uni_modules/stars-UniTS-SQLite-ORM/components/stars-UniTS-SQLite-ORM/dbOrm/config/dbconfig';

export class TransactionManager {
	private transactionStarted : boolean = false;

	async beginTransaction() : Promise<void> {
		if (!this.transactionStarted) {
			// 执行开启事务的SQL语句
			await executeSql(DB_NAME, "BEGIN TRANSACTION");
			this.transactionStarted = true;
			console.log("开启事务");
		}
	}

	async commitTransaction() : Promise<void> {
		if (this.transactionStarted) {
			// 执行提交事务的SQL语句
			await executeSql(DB_NAME, "COMMIT");
			this.transactionStarted = false;
			console.log("事务提交");
		}
	}

	async rollbackTransaction() : Promise<void> {
		if (this.transactionStarted) {
			// 执行回滚事务的SQL语句
			await executeSql(DB_NAME, "ROLLBACK");
			this.transactionStarted = false;
			console.log("事务回滚");
		}
	}
}